import javax.print.DocFlavor;
import java.util.*;
public class HouseView {
    private HouseService service;
    private  Scanner scan;
    public HouseView( ) {
        service = new HouseService();
        scan = new Scanner(System.in);
    }
    //显示
    public void viewHouse() {
        System.out.println("欢迎使用房屋信息管理系统");
        System.out.println("            1.添加房源");
        System.out.println("            2.查找房屋");
        System.out.println("            3.修改房屋信息");
        System.out.println("            4.删除房屋");
        System.out.println("            5.房屋列表");
        System.out.println("            6.退出");
        System.out.println("请选项（1-6）：");
    }

    public void run() {
        while (true) {
            viewHouse();
            int choice = scan.nextInt();

            switch (choice) {
                case 1:
                    addHouse();
                    break;
                case 2:
                    findHouse();
                    break;
                case 3:
                    updateHouse();
                    break;
                case 4:
                    deleteHouse();
                    break;
                case 5:
                    service.printHouseList();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("无效输入，请重新输入");
            }
        }
    }
//添加信息
    private void addHouse(){
        System.out.println("------------添加房屋------------");
        scan.nextLine();
        System.out.println("请输入姓名");
        String name = scan.nextLine();
        System.out.println("请输入电话");
        String phone = scan.nextLine();
        System.out.println("请输入房屋地址");
        String address = scan.nextLine();
        System.out.println("请输入房屋月租");
        double price = scan.nextDouble();
        scan.nextLine();
        System.out.println("请输入状态(未出租/已出租)：");
        String status = scan.nextLine();
        House house = new House(name, phone, address, price, status);
        service.addHouse(house);
        System.out.println("-----------------房屋信息添加成功！");
    }
//查找信息
    private void findHouse() {
        while (true){
        System.out.println("-------------------------- 查找房源 --------------------------");
        scan.nextLine();
        System.out.print("请输入要查找的房屋名称：");
        String name = scan.next();
        House house = service.FinHouseName(name);
        if ( house != null ) {
                System.out.println(house.getName() + "\t" + house.getPhone() + "\t" + house.getAddress()
                        + "\t" + house.getPrice() + "\t" + house.getStatus());
        }
        else {
            System.out.println("未找到该房屋信息。");
            break;
        }
        }
    }

    //修改信息
    private void updateHouse() {
        scan.nextLine();
        System.out.println("请输入要修改的房屋名称：");
        String name = scan.nextLine();
        System.out.println("请输入要修改的电话");
        String phone = scan.nextLine();
        System.out.println("请输入新的房屋地址：");
        String address = scan.nextLine();
        System.out.println("请输入新的月租：");
        double price = scan.nextDouble();
        scan.nextLine();
        System.out.println("请输入新的状态(未出租/已出租)：");
        String status = scan.nextLine();
        service.updateHouse(name, phone, address, price, status);
        System.out.println("房屋信息修改成功！");
    }
//删除信息
private void deleteHouse() {
    System.out.println("请输入要删除的房屋名称：");
    String name = scan.next();
    System.out.println("是否确认删除（Y/N）:请小心选择：");
    System.out.println("请输入您的选择Y/N:");
    String FN= scan.nextLine();
    if (FN=="Y" ||FN=="y"){
        service.deleteHouse(name);
        System.out.println("-------------删 除 成 功！----------------");
    }
    else {
        viewHouse();
    }
}
}
